<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>小兔鲜儿 - 新鲜 惠民 快捷!</title>
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="renderer" content="webkit">
  <link rel="shortcut icon" href="./favicon.ico">
  <link rel="stylesheet" href="./css/common.css">
  <link rel="stylesheet" href="./css/register.css">
  <link rel="stylesheet" href="https://at.alicdn.com/t/font_2143783_iq6z4ey5vu.css">
  <style>
    
  </style>
</head>

<body>
  <!-- 项部导航 -->
  <div class="xtx_topnav">
    <div class="wrapper">
      <!-- 顶部导航 -->
      <ul class="xtx_navs">
        <li>
          <a href="./login.html">请先登录</a>
        </li>
        <li>
          <a href="./register.html">免费注册</a>
        </li>
        <li>
          <a href="./center-order.html">我的订单</a>
        </li>
        <li>
          <a href="./center.html">会员中心</a>
        </li>
        <li>
          <a href="javascript:;">帮助中心</a>
        </li>
        <li>
          <a href="javascript:;">在线客服</a>
        </li>
        <li>
          <a href="javascript:;">
            <i class="mobile sprites"></i>
            手机版
          </a>
        </li>
      </ul>
    </div>
  </div>
  <!-- 头部 -->
  <div class="xtx_header">
    <div class="wrapper">
      <!-- 网站Logo -->
      <h1 class="xtx_logo"><a href="/">小兔鲜儿</a></h1>
      <!-- 主导航 -->
      <div class="xtx_navs">
        <ul class="clearfix">
          <li>
            <a href="./index.html">首页</a>
          </li>
          <li>
            <a href="./category01.html">生鲜</a>
          </li>
          <li>
            <a href="./category01.html">美食</a>
          </li>
          <li>
            <a href="./category01.html">餐厨</a>
          </li>
          <li>
            <a href="./category01.html">电器</a>
          </li>
          <li>
            <a href="./category01.html">居家</a>
          </li>
          <li>
            <a href="./category01.html">洗护</a>
          </li>
          <li>
            <a href="./category01.html">孕婴</a>
          </li>
          <li>
            <a href="./category01.html">服装</a>
          </li>
        </ul>
      </div>
      <!-- 站内搜索 -->
      <div class="xtx_search clearfix">
        <!-- 购物车 -->
        <a href="./cart-none.html" class="xtx_search_cart sprites">
          <i>2</i>
        </a>
        <!-- 搜索框 -->
        <div class="xtx_search_wrapper">
          <input type="text" placeholder="搜一搜" onclick="location.href='./search.html'">
        </div>
      </div>
    </div>
  </div>
  <div class="xtx-wrapper">
    <div class="container">
      <!-- 卡片 -->
      <div class="xtx-card">
        <h3>新用户注册</h3>
        <form class="xtx-form" action="./login.html">
          <div data-prop="username" class="xtx-form-item">
            <span class="iconfont icon-zhanghao"></span>
            <input name="username" type="text" placeholder="设置用户名称">
            <span class="msg"></span>
          </div>
          <div data-prop="phone" class="xtx-form-item">
            <span class="iconfont icon-shouji"></span>
            <input name="phone" type="text" placeholder="输入手机号码  ">
            <span class="msg"></span>
          </div>
          <div data-prop="code" class="xtx-form-item">
            <span class="iconfont icon-zhibiaozhushibiaozhu"></span>
            <input name="code" type="text" placeholder="短信验证码">
            <span class="msg"></span>
            <a class="code" href="javascript:;" >发送验证码</a>
          </div>
          <div data-prop="password" class="xtx-form-item">
            <span class="iconfont icon-suo"></span>
            <input name="password" type="password" placeholder="设置6至20位字母、数字和符号组合">
            <span class="msg"></span>
          </div>
          <div data-prop="confirm" class="xtx-form-item">
            <span class="iconfont icon-suo"></span>
            <input name="confirm" type="password" placeholder="请再次输入上面密码">
            <span class="msg"></span>
          </div>
          <div class="xtx-form-item pl50">
            <i class="checkBtn iconfont icon-queren"></i>
            <span class="checkBtn">已阅读并同意</span><i>《用户服务协议》</i>
          </div>
          <div class="xtx-form-item">
            <button class="submit">下一步</button>
            <!-- <a class="submit" href="javascript:;">下一步</a> -->
          </div>
        </form>
      </div>
    </div>
  </div>
  <!-- 公共底部 -->
  <div class="xtx_footer clearfix">
    <div class="wrapper">
      <!-- 联系我们 -->
      <div class="contact clearfix">
        <dl>
          <dt>客户服务</dt>
          <dd class="chat">在线客服</dd>
          <dd class="feedback">问题反馈</dd>
        </dl>
        <dl>
          <dt>关注我们</dt>
          <dd class="weixin">公众号</dd>
          <dd class="weibo">微博</dd>
        </dl>
        <dl>
          <dt>下载APP</dt>
          <dd class="qrcode">
            <img src="./uploads/qrcode.jpg">
          </dd>
          <dd class="download">
            <span>扫描二维码</span>
            <span>立马下载APP</span>
            <a href="javascript:;">下载页面</a>
          </dd>
        </dl>
        <dl>
          <dt>服务热线</dt>
          <dd class="hotline">
            400-0000-000
            <small>周一至周日 8:00-18:00</small>
          </dd>
        </dl>
      </div>
    </div>
    <!-- 其它 -->
    <div class="extra">
      <div class="wrapper">
        <!-- 口号 -->
        <div class="slogan">
          <a href="javascript:;" class="price">价格亲民</a>
          <a href="javascript:;" class="express">物流快捷</a>
          <a href="javascript:;" class="quality">品质新鲜</a>
        </div>
        <!-- 版权信息 -->
        <div class="copyright">
          <p>
            <a href="javascript:;">关于我们</a>
            <a href="javascript:;">帮助中心</a>
            <a href="javascript:;">售后服务</a>
            <a href="javascript:;">配送与验收</a>
            <a href="javascript:;">商务合作</a>
            <a href="javascript:;">搜索推荐</a>
            <a href="javascript:;">友情链接</a>
          </p>
          <p>CopyRight &copy; 小兔鲜儿</p>
        </div>
      </div>
    </div>
  </div>
  <script>
    // 获取对象
    const username = document.querySelector('[name=username]');
    const phone = document.querySelector('[name=phone]');
    const code = document.querySelector('[name=code]');
    const pwd = document.querySelector('[name=password]');
    const confirm = document.querySelector('[name=comfirm]');
    const codeBtn = document.querySelector('.code');
    const checkBox = document.querySelector('.pl50');

    const msgs = document.querySelectorAll('.msg');
    const form = document.querySelector('form');
    const formItems = document.querySelectorAll('.xtx-form input')
    
    console.log(formItems)
    
    // 验证用户名
    username.addEventListener('change',function(){
      username.nextElementSibling.innerHTML = ''
      // 正则 : 字母，数字，下划线，减号，6-16
      let reg = /^[A-Za-z0-9_-]{6,16}$/;
      if(!reg.test(username.value)){ // 注意要取反
        // 提示用户 用户名不合规
        username.nextElementSibling.innerHTML = '用户名不合规'
      }
    })

    // 验证手机号
    phone.addEventListener('change',function(){
      phone.nextElementSibling.innerHTML = ''
      // 正则 : 字母，数字，下划线，减号，6-16
      let reg = /^1[34578][0-9]{9}$/;
      if(!reg.test(phone.value)){ // 注意要取反
        // 提示用户 用户名不合规
        phone.nextElementSibling.innerHTML = '手机号不合规'
      }
    })

    // 验证验证码
    code.addEventListener('change',function(){
      code.nextElementSibling.innerHTML = ''
      let reg = /^[0-9]{4}$/;
      if(!reg.test(code.value)){ // 注意要取反
        // 提示用户 用户名不合规
        code.nextElementSibling.innerHTML = '验证码不合规'
      }
    })

    // 验证密码
    pwd.addEventListener('change',function(){
      pwd.nextElementSibling.innerHTML = ''
      let reg = /^.{6,20}$/;
      if(!reg.test(pwd.value)){ // 注意要取反
        // 提示用户 用户名不合规
        pwd.nextElementSibling.innerHTML = '密码不合规'
      }
    })

    // 重复密码验证
    pwd.addEventListener('change',function(){
        pwd.nextElementSibling.innerHTML = ''
      if(pwd.value !== confirm.value){ // 注意要取反
        // 提示用户 用户名不合规
        pwd.nextElementSibling.innerHTML = '两次密码不一致'
      }
    })

    // 验证码倒计时
    codeBtn.addEventListener('click',function getCode(){
      
      codeBtn.innerHTML = '5秒后重新获取'
      // 更改样式
      codeBtn.classList.add('ing');
      // 要将事件解绑，不让用户重复触发点击事件
      codeBtn.removeEventListener('click',getCode)

      // 定时器
      let i = 5;
      let timer = setInterval(function(){
        i--;
        codeBtn.innerHTML = `${i}秒后重新获取`
        // 判断定时器的停止时机
        if(i === 0){
          // 停止定时器
          clearInterval(timer);
          // 将内容改回未点击之前的内容
          codeBtn.innerHTML = '发送验证码'
          // 再次将事件绑定回去
          codeBtn.addEventListener('click',getCode);
          codeBtn.classList.remove('ing');
        }
      },1000)
    })
    // 服务协议按钮
    let checked = false;
    checkBox.addEventListener('click',function(e){
      if(e.target.classList.contains('checkBtn') == true){
        this.querySelector('.icon-queren').classList.toggle('icon-queren2')    // 切换类名
        checked = !checked // 对当前值进行取反
      }
    })

    // 提交表单条件
    
    form.addEventListener('submit',function(e){
      // 不能提交的情况
      // 如果有不合规的情况，不能跳转
      
      for (let i = 0; i < msgs.length; i++) {
        if(msgs[i].innerHTML !== ''){  // 不为空 说明msg是有不合规的提示
          e.preventDefault();  // 阻止默认行为
          return alert('请完善表单的填写')
        }
      }

      // 如果有空白内容，不能跳转
      for (let i = 0; i < formItems.length; i++) {
        if(formItems[i].value.trim() === ''){
          e.preventDefault();  // 阻止默认行为
          return alert('表单内容不能为空')
        }
      }

   
      // 服务协议未勾选，不能跳转
      if(!checked){   // 有勾选  !true  --> false
        e.preventDefault();  // 阻止默认行为
        return alert('请勾选服务协议')
      }

      // 需要获取数组
      let users = JSON.parse(localStorage.getItem('users')) || [];

      // 添加数据至浏览器中储存
      let user = {
        username: username.value,
        phone: phone.value,
        pwd: pwd.value
      }

      // 添加进数组
      users.push(user)

      // 重新存入浏览器
      localStorage.setItem('users',JSON.stringify(users))

    })
    
    
  </script>
</body>

</html>